package com.hieu.android.designpattern.demo.main;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.hieu.android.designpattern.R;
import com.hieu.android.designpattern.observer.ObserverMainActivity;

public class MainActivity extends Activity {

	ActionBar mActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mActionBar = getActionBar();
		if (mActionBar != null) {
			mActionBar.show();
			mActionBar.setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		createActionMenu(menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:
			Toast.makeText(getApplicationContext(), 
					"Choose Home", Toast.LENGTH_LONG).show();
			Intent i1 = new Intent(this, MainActivity.class);
			i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i1);
			break;
		case 0:
			break;

		case 1:
			Toast.makeText(getApplicationContext(), 
					"Choose Observer Pattern", Toast.LENGTH_LONG).show();
			Intent i2 = new Intent(this, ObserverMainActivity.class);
			startActivity(i2);
			break;
			
		case 2:
			Toast.makeText(getApplicationContext(), 
					"Choose Factory Pattern", Toast.LENGTH_LONG).show();
			
			break;
		case 3:
			Toast.makeText(getApplicationContext(), 
					"Choose MVC Pattern", Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void createActionMenu(Menu menu) {
		
		MenuItem mnu1 = menu.add(0, 0, 0, R.string.menu_action_item_Home);
		{
			mnu1.setIcon(R.drawable. ic_launcher);
			mnu1.setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
		}
		MenuItem mnu2 = menu.add(0, 1, 1, R.string.menu_action_item_observer);
		{
			mnu2.setIcon(R.drawable. ic_launcher);
			mnu2.setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
		}
		MenuItem mnu3 = menu.add(0, 2, 2, R.string.menu_action_item_factory);
		{
			mnu3.setIcon(R.drawable. ic_launcher);
			mnu3.setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
		}
		MenuItem mnu4 = menu.add(0, 3, 3, R.string.menu_action_item_MVC);
		{
			mnu4.setIcon(R.drawable. ic_launcher);
			mnu4.setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
		}
	}
}
